package carassius.GUI;

import carassius.DAL.AquariumTypeRow;
import carassius.DAL.Database;
import java.awt.Dialog;
import java.awt.Frame;
import java.sql.SQLException;
import java.util.ResourceBundle;
import javax.swing.JOptionPane;

/**
 *
 * @author siebz0r
 */
public class AquariumTypeForm
		extends javax.swing.JDialog
{
	//todo: merge constructors
	public AquariumTypeForm(Frame parent)
	{
		super(parent, true);
		initComponents();
	}

	public AquariumTypeForm(Dialog parent)
	{
		super(parent, true);
		initComponents();
	}

	/** This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        uxIdPrefixLabel = new javax.swing.JLabel();
        uxIdLabel = new javax.swing.JLabel();
        uxNamePrefixLabel = new javax.swing.JLabel();
        uxNameTextField = new javax.swing.JTextField();
        uxButtonPanel = new javax.swing.JPanel();
        uxSaveButton = new javax.swing.JButton();
        uxCancelButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(carassius.CarassiusApp.class).getContext().getResourceMap(AquariumTypeForm.class);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setName("Form"); // NOI18N
        getContentPane().setLayout(new java.awt.GridBagLayout());

        uxIdPrefixLabel.setText(resourceMap.getString("uxIdPrefixLabel.text")); // NOI18N
        uxIdPrefixLabel.setName("uxIdPrefixLabel"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        getContentPane().add(uxIdPrefixLabel, gridBagConstraints);

        uxIdLabel.setText(resourceMap.getString("uxIdLabel.text")); // NOI18N
        uxIdLabel.setName("uxIdLabel"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 0, 5, 5);
        getContentPane().add(uxIdLabel, gridBagConstraints);

        uxNamePrefixLabel.setText(resourceMap.getString("uxNamePrefixLabel.text")); // NOI18N
        uxNamePrefixLabel.setName("uxNamePrefixLabel"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.ABOVE_BASELINE_LEADING;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 0, 5);
        getContentPane().add(uxNamePrefixLabel, gridBagConstraints);

        uxNameTextField.setText(resourceMap.getString("uxNameTextField.text")); // NOI18N
        uxNameTextField.setName("uxNameTextField"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.FIRST_LINE_START;
        gridBagConstraints.insets = new java.awt.Insets(5, 0, 0, 5);
        getContentPane().add(uxNameTextField, gridBagConstraints);

        uxButtonPanel.setName("uxButtonPanel"); // NOI18N
        uxButtonPanel.setLayout(new java.awt.GridBagLayout());

        uxSaveButton.setText(resourceMap.getString("uxSaveButton.text")); // NOI18N
        uxSaveButton.setName("uxSaveButton"); // NOI18N
        uxSaveButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                uxSaveButtonActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        uxButtonPanel.add(uxSaveButton, gridBagConstraints);

        uxCancelButton.setText(resourceMap.getString("uxCancelButton.text")); // NOI18N
        uxCancelButton.setName("uxCancelButton"); // NOI18N
        uxCancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                uxCancelButtonActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 0, 5, 5);
        uxButtonPanel.add(uxCancelButton, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.SOUTHEAST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        getContentPane().add(uxButtonPanel, gridBagConstraints);

        pack();
    }// </editor-fold>//GEN-END:initComponents

	private void uxSaveButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_uxSaveButtonActionPerformed
	{//GEN-HEADEREND:event_uxSaveButtonActionPerformed
		String name = uxNameTextField.getText();

		try
		{
			AquariumTypeRow row = Database.getAquariumTypeTable().add(name);

			if (row != null)
			{
				uxIdLabel.setText(Integer.toString(row.getId()));
				JOptionPane.showMessageDialog(this,
						ResourceBundle.getBundle("carassius.resources.Messages").getString("AquariumTypeAdded"),
						ResourceBundle.getBundle("carassius.resources.Messages").getString("AquariumTypeAddedTitle"),
						JOptionPane.INFORMATION_MESSAGE);
				dispose();
			}
		}
		catch (SQLException ex)
		{
			System.out.println(ex.toString());
		}

		JOptionPane.showMessageDialog(this,
				ResourceBundle.getBundle("carassius.resources.Messages").getString("AddAquariumTypeError"),
				ResourceBundle.getBundle("carassius.resources.Messages").getString("AddAquariumTypeErrorTitle"),
				JOptionPane.ERROR_MESSAGE);
	}//GEN-LAST:event_uxSaveButtonActionPerformed

	private void uxCancelButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_uxCancelButtonActionPerformed
	{//GEN-HEADEREND:event_uxCancelButtonActionPerformed
		dispose();
	}//GEN-LAST:event_uxCancelButtonActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel uxButtonPanel;
    private javax.swing.JButton uxCancelButton;
    private javax.swing.JLabel uxIdLabel;
    private javax.swing.JLabel uxIdPrefixLabel;
    private javax.swing.JLabel uxNamePrefixLabel;
    private javax.swing.JTextField uxNameTextField;
    private javax.swing.JButton uxSaveButton;
    // End of variables declaration//GEN-END:variables
}
